package com.xafc.demo.sys.controller;

import com.xafc.demo.framework.result.ApiResult;
import com.xafc.demo.security.utils.UserHolderUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class TestController {


    @RequestMapping("/test/param")
    @ResponseBody
    public Map<String, Object> testParam(
            @RequestParam Map<String, String> params,
            @RequestHeader Map<String, String> headers,
            @RequestBody String body
    ){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("params", params);
        resultMap.put("headers", headers);
        resultMap.put("body", body);
        return resultMap;
    }

    @RequestMapping("/test")
    @ResponseBody
    public ApiResult test(){
        Map<String, Object> data = new HashMap<>();
        data.put("page", "Index->Normal");

        // 自己保存的用户信息
        data.put("Self --> currentUserOfUsername", UserHolderUtils.getUser().getUsername());
        // Spring上下文中的用户信息
        UserDetails userDetails = getHolderUserInfo();
        data.put("Spring --> currentUserOfAuthorities", userDetails.getAuthorities());
        data.put("Spring --> currentUserOfUsername", userDetails.getUsername());
        return ApiResult.success(data);
    }


    @PreAuthorize("hasAuthority('admin')")
    @RequestMapping("/admin")
    @ResponseBody
    public ApiResult admin(){
        Map<String, Object> data = new HashMap<>();
        data.put("page", "Index->Admin");

        // 自己保存的用户信息
        data.put("Self --> currentUserOfUsername", UserHolderUtils.getUser().getUsername());
        // Spring上下文中的用户信息
        UserDetails userDetails = getHolderUserInfo();
        data.put("Spring --> currentUserOfAuthorities", userDetails.getAuthorities());
        data.put("Spring --> currentUserOfUsername", userDetails.getUsername());
        return ApiResult.success(data);
    }

    @PreAuthorize("hasAuthority('normal')")
    @RequestMapping("/normal")
    @ResponseBody
    public ApiResult normal(){
        Map<String, Object> data = new HashMap<>();
        data.put("page", "Index->Normal");

        // 自己保存的用户信息
        data.put("Self --> currentUserOfUsername", UserHolderUtils.getUser().getUsername());
        // Spring上下文中的用户信息
        UserDetails userDetails = getHolderUserInfo();
        data.put("Spring --> currentUserOfAuthorities", userDetails.getAuthorities());
        data.put("Spring --> currentUserOfUsername", userDetails.getUsername());
        return ApiResult.success(data);
    }

    private UserDetails getHolderUserInfo() {
        final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails){
            return (UserDetails) principal;
        }
        return null;
    }

}
